package com.citcc.api.netty.handle;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.citcc.api.netty.EchargeConstants;
import com.citcc.api.netty.bean.ConsuMessage;

/**
 * 反馈handler负责处理收到的反馈信息
 * @author think
 *
 */
public class RecevieHandler extends SimpleChannelHandler {
    private static Logger LOG = LoggerFactory.getLogger(RecevieHandler.class);


    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e){
        ConsuMessage msg = (ConsuMessage) e.getMessage();
        ChannelBuffer body = msg.getMsgBody();
        LOG.error("应答----------------" + "0x" + Integer.toHexString(msg.getMsgId()));
        if(msg.getMsgId() == EchargeConstants.LOGIN_C){
            int transType = body.readByte();
            int loginState = body.readByte();
            if(loginState == EchargeConstants.LOGIN_SUCCESS_C){
                LOG.info("------------------登录成功");
            }else{
                LOG.error("------------------登录异常，请检查");
            }
        }else if(msg.getMsgId() == EchargeConstants.HEART_C){//心跳响应
            int transType = body.readByte();
            int heartState = body.readByte();
            if(heartState == EchargeConstants.HEART_SUCCESS_C){
                LOG.info("------------------心跳响应成功");
            }else{
                LOG.error("------------------心跳响应异常，请检查");
            }
        }
    }
}
